home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlib.zip / STRKEY.C < prev    next >
Text File  |  1993-01-04  |  5KB  |  150 lines

  1.  
  2. /*  File   : strkey.c
  3.     Author : Richard A. O'Keefe.
  4.     Updated: 20 April 1984
  5.     Defines: strkey()
  6.  
  7.     strkey(dst, head, tail, options)
  8.         copies tail-head characters from head to dst according to the
  9.         options.  If tail is NullS, it copies up to the terminating
  10.         NUL of head.  This function is meant for doing comparisons as
  11.         by sort(1).  The options are thus a string of characters
  12.         taken from "bdfin".  In case the options came from somewhere
  13.         else other letters are ignored.
  14.  
  15.         -b: leading layout characters are not copied.
  16.  
  17.         -d: only letters, digits, and blanks are copied.
  18.         -i: only graphic characters (32..126) are copied.
  19.         -n: a numeric string is copied.
  20.             These options are incompatible, and the last is taken.
  21.  
  22.         -f: upper case letters are copied as lower case.
  23.  
  24.     The question of what to do with a numeric string is  an  interesting
  25.     one,  and  I  don't claim that this is a brilliant answer.  However,
  26.     the solution used here does mean that the  caller  can  compare  two
  27.     strings as strings without needing to know that they are numeric.  A
  28.     number  is  copied  as  <sign><9  digits>.<remaining  digits>, where
  29.     <sign> is '-' for a negative number and '0' for a  positive  number.
  30.     The magic number 9 is defined to be DigitMagic.
  31.  
  32.     The idea is that to compare two lines using the keys
  33.         -tx +m1.n1<flags> -m2.n2
  34.     you do
  35.         h1 = strfield(line1, m1, n1, 0, 'x');
  36.         t1 = strfield(h1, 1, 0, 0, 'x');
  37.         strkey(buff1, h1, t1, "flags");
  38.         h2 = strfield(line2, m2, n2, 0, 'x');
  39.         t2 = strfield(h2, 1, 0, 0, 'x');
  40.         strkey(buff2, h2, t2, "flags");
  41.         ... strcmp(buff1, buff2) ...
  42.  
  43.     The point of all this, of course, is to make it easier to write new
  44.     utilities which are compatible with sort(1) than ones which are not.
  45. */
  46.  
  47. #include "strings.h"
  48.  
  49. #define DigitMagic 9
  50.  
  51. char *strkey(dst, head, tail, flags)
  52.     register char *dst, *head, *tail;
  53.     char *flags;
  54.     {
  55.         register int c;
  56.         int b = 0;      /* b option? */
  57.         int f = 0;      /* f option? */
  58.         int k = 0;      /* 3->n, 2->d, 1->i, 0->none of them */
  59.  
  60.         while (*flags) switch (*flags++|32) {
  61.             case 'b':   b++;    break;
  62.             case 'f':   f++;    break;
  63.             case 'i':   k = 1;  break;
  64.             case 'd':   k = 2;  break;
  65.             case 'n':   k = 3;  break;
  66.             default : /*ignore*/break;
  67.         }
  68.         flags = dst;    /* save return value */
  69.  
  70.         if (tail == NullS) for (tail = head; *tail; tail++) ;
  71.  
  72.         if (b) while (head != tail && *head <= ' ') head++;
  73.  
  74.         switch (k) {
  75.             case 0:
  76.                 if (f) {
  77.                     while (head != tail) {
  78.                         c = *head++;
  79.                         if (c >= 'A' && c <= 'Z') c |= 32;
  80.                         *dst++ = c;
  81.                     }
  82.                 } else {
  83.                     while (head != tail) *dst++ = *head++;
  84.                 }
  85.                 break;
  86.             case 1:
  87.                 if (f) {
  88.                     while (head != tail) {
  89.                         c = *head++;
  90.                         if (c >= 32 && c <= 126) {
  91.                             if (c >= 'A' && c <= 'Z') c |= 32;
  92.                             *dst++ = c;
  93.                         }
  94.                     }
  95.                 } else {
  96.                     while (head != tail) {
  97.                         c = *head++;
  98.                         if (c >= 32 && c <= 126) *dst++ = c;
  99.                     }
  100.                 }
  101.                 break;
  102.             case 2:
  103.                 if (f) f = 32;
  104.                 while (head != tail) {
  105.                     c = *head++;
  106.                     if (c >= '0' && c <= '9' ||  c >= 'a' && c <= 'z' || c == '
  107. ') {
  108.                         *dst++ = c;
  109.                     } else
  110.                     if (c >= 'A' && c <= 'Z') {
  111.                         *dst++ = c|f;
  112.                     }
  113.                 }
  114.                 break;
  115.             case 3:
  116.                 if (*head == '-' && head != tail) {
  117.                     *dst++ = *head++;
  118.                     head++;
  119.                 } else {
  120.                     *dst++ = '0';
  121.                 }
  122.                 b = 0;
  123.                 while (head != tail) {
  124.                     c = *head;
  125.                     if (c < '0' || c > '9') break;
  126.                     b++, head++;
  127.                 }
  128.                 f = DigitMagic-b;
  129.                 while (--f >= 0) *dst++ = '0';
  130.                 head -= b;
  131.                 while (--b >= 0) *dst++ = *head++;
  132.                 if (*head == '.' && head != tail) {
  133.                     *dst++ = *head++;
  134.                     while (head != tail) {
  135.                         c = *head++;
  136.                         if (c < '0' || c > '9') break;
  137.                         *dst++ = c;
  138.                     }
  139.                     /* now remove trailing 0s and possibly the '.' as well */
  140.                     while (*--dst == '0') ;
  141.                     if (*dst != '.') dst++;
  142.                 }
  143.                 break;
  144.         }
  145.         *dst = NUL;
  146.         return flags;   /* saved initial value of dst */
  147.     }
  148.  
  149.  
  150.